//
//  HMNewFeatureController.m
//  G05爱鲜蜂
//
//  Created by 源 何 on 16/10/15.
//  Copyright © 2016年 何源. All rights reserved.
//

#import "HMNewFeatureController.h"
#import "HMMainViewController.h"

@interface HMNewFeatureController ()

//图片数组
@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation HMNewFeatureController

static NSString * const reuseIdentifier = @"HMNewFeatureControllerID";

#pragma mark
#pragma mark -- 懒加载
- (UICollectionViewFlowLayout *)flowLayout{
    if (nil == _flowLayout) {
        
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        _flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
        
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _flowLayout.minimumLineSpacing = 0;
        
        _flowLayout.minimumInteritemSpacing = 0;
    }
    return _flowLayout;
}

- (NSArray *)dataArray{
    if (nil == _dataArray) {
        _dataArray = @[[UIImage imageNamed:@"guide_40_1"], [UIImage imageNamed:@"guide_40_2"], [UIImage imageNamed:@"guide_40_3"], [UIImage imageNamed:@"guide_40_4"]];
    }
    return _dataArray;
}

#pragma mark
#pragma mark -- 重写init
- (instancetype)init{
    
    return [super initWithCollectionViewLayout:self.flowLayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.bounces = NO;
    
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    _pageControl = [[UIPageControl alloc] init];
    _pageControl.numberOfPages = self.dataArray.count;
    _pageControl.currentPage = 0;
    [_pageControl setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
    [_pageControl setPageIndicatorTintColor:[UIColor grayColor]];
    
    [self.view addSubview:_pageControl];
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).multipliedBy(1.92);
    }];
}

#pragma mark
#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:self.dataArray[indexPath.item]];
    
    [cell.contentView addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(cell.contentView);
    }];
    
    if (indexPath.row == 3) {
        UIButton *beginBtn = [[UIButton alloc] init];
        
        [beginBtn setImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal];
        [cell.contentView addSubview:beginBtn];
        
        [beginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(cell.contentView);
            make.centerY.equalTo(cell).multipliedBy(1.7);
        }];
        
        [beginBtn addTarget:self action:@selector(jumpToHome) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSInteger pageNum = (scrollView.contentOffset.x + scrollView.bounds.size.width * 0.5) / scrollView.bounds.size.width;
    
    //2. 给pageControl进行设置当前页
    _pageControl.currentPage = pageNum;
}

- (void)jumpToHome{
    // 切换 window的根控制器
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

    keyWindow.rootViewController = [[HMMainViewController alloc] init];
}



@end
